from NewStudentManageSys.settings.dev import TEMPLATE_INFO
from foreign_university.models import ForeignUniversity
from order.models import ProjectOrderRelation, Order
from user_info.models import UserInfo
from utils.wx_utils.weixin_fouctions import WxInterfaceUtil
import datetime


def send_confirm_inform(student, order_info, user_info):
    """
    cc确认缴费信息后，待财务确认提醒
    :param student: 学生对象
    :param order_info: 订单对象
    :param user_info: cc对象
    :return: 微信推送给财务
    """
    user_list = UserInfo.objects.filter(department_name='财务部')
    order_chart = ProjectOrderRelation.objects.filter(order=order_info).values('shopping_cart__project__name')
    project_info = ','.join(order.get('shopping_cart__project__name') for order in order_chart)
    for user in user_list:
        data = {
            "touser": user.openid,
            "template_id": TEMPLATE_INFO['CHECK_INFORM'],
            "data": {
                "first": {
                    "value": "%s你好，有收入信息，请及时审核！" % user.name,
                    "color": "#173177"
                },
                "keyword1": {
                    "value": student.name,
                    "color": "#173177"
                },
                "keyword2": {
                    "value": student.id,
                    "color": "#173177"
                },
                "keyword3": {
                    "value": project_info,
                    "color": "#173177"
                },
                "keyword4": {
                    "value": order_info.confirm_fee,
                    "color": "#173177"
                },
                "keyword5": {
                    "value": order_info.order_number,
                    "color": "#173177"
                },
                "remark": {
                    "value": "CC(%s)已确认学生缴费订单，请及时处理。" % user_info.name,
                    "color": "#173177"
                }
            }
        }
        return WxInterfaceUtil.send_template_inform(data)


def send_register_inform(student, validated_data):
    """
    新学生进入系统
    :param student: 学生对象
    :param validated_data: 学生信息字典
    :return: 微信推送给cc
    """
    user = UserInfo.objects.filter(id=validated_data['cc']).first()
    data = {
        "touser": user.openid,
        "template_id": TEMPLATE_INFO['ADMIN_REGISTER_INFORM'],
        "data": {
            "first": {
                "value": "%s你好，一条新数据刚刚进入学生系统，已分配给你。" % user.name,
                "color": "#173177"
            },
            "keyword1": {
                "value": student.id,
                "color": "#173177"
            },
            "keyword2": {
                "value": validated_data['name'],
                "color": "#173177"
            },
            "keyword3": {
                "value": student.channel_name,
                "color": "#173177"
            },
            "remark": {
                "value": "微信号 : %s,\n邮箱 : %s,\n学校 : %s.\n请及时主动联系跟进，并在2个工作日内进入系统完成相关备注。" % (
                    validated_data['wechat'], validated_data['email'], validated_data['cschool']),
                "color": "#173177"
            }
        }
    }
    return WxInterfaceUtil.send_template_inform(data)


def send_payment_inform(order_payment):
    """
    学生上传缴费凭据，推送给销售
    :param order_payment: 订单缴费对象
    :return:
    """
    user_list = UserInfo.objects.filter(id=order_payment.order.student.cc_id)
    order_cart = ProjectOrderRelation.objects.filter(order=order_payment.order).values('project__name')
    project_info = ','.join(order.get('project__name') for order in order_cart)
    for user in user_list:
        data = {
            "touser": user.openid,
            "template_id": TEMPLATE_INFO['ORDER_NOTIFICATION'],
            "data": {
                "first": {
                    "value": "缴费成功通知：\n\t%s你好，有学员上传缴费凭据，请及时确认。" % (user.name,),
                    "color": "#173177"
                },
                "keyword1": {
                    "value": order_payment.order.student.name,
                    "color": "#173177"
                },
                "keyword2": {
                    "value": "项目: %s" % project_info,
                    "color": "#173177"
                },
                "keyword3": {
                    "value": str(order_payment.order.order_amount),
                    "color": "#173177"
                },
                "keyword4": {
                    "value": dict(Order.ORDER_STATUS).get(order_payment.order.order_status),
                    "color": "#173177"
                },
                "keyword5": {
                    "value": "%s" % str(order_payment.create_time)[:19],
                    "color": "#173177"
                },
                "remark": {
                    "value": "",
                    "color": "#173177"
                }
            }
        }
        return WxInterfaceUtil.send_template_inform(data)


def send_mission_inform(student):
    """
    派单给cc或者学服
    :param student: 学生对象
    :return: 微信推送给cc 或学服
    """
    user_list = UserInfo.objects.filter(id__in=[student.cc, student.server])
    result = ForeignUniversity.objects.filter(school=student.cschool).first()
    student_info = "微信号 : %s\n所在学校 : %s\n" % (student.wechat, student.cschool)
    for user in user_list:
        if user.department_id.name == '销售部':
            title = "%s你好，你有一个学生刚刚确认成功缴费，系统已指定学服老师%s后续跟进" % (user.name, result.server.name)
            content = "%s请尽快将该学生微信名片推送给学服%s，并在系统中备注好前期你与学生沟通过程中涉及学服的相关内容，方便学服为学生提供后续服务。" % (
            student_info, result.server.name)
        else:
            title = "%s你好，一个新数据刚刚分配给你，等待你的学服服务。" % user.name
            content = "%s请尽快主动添加学生微信名片为好友，并进入系统后台查看上一流程里市场负责人的相关备注，以便你高效地为学生提供学服服务。" % student_info
        data = {
            "touser": user.openid,
            "template_id": TEMPLATE_INFO['MISSION_INFORM'],
            "data": {
                "first": {
                    "value": title,
                    "color": "#173177"
                },
                "keyword1": {
                    "value": '系统分配',
                    "color": "#173177"
                },
                "keyword2": {
                    "value": student.channel_name,
                    "color": "#173177"
                },
                "keyword3": {
                    "value": student.name,
                    "color": "#173177"
                },
                # "keyword4": {
                #     "value": "",
                #     "color": "#173177"
                # },
                "remark": {
                    "value": content,
                    "color": "#173177"
                }
            }
        }
        return WxInterfaceUtil.send_template_inform(data)

def cc_reject_student(instance):
    """
    cc驳回学生
    :param instance: 学生对象
    :return: 微信推送给学生
    """
    data = {
        "touser": instance.user.openid,
        "template_id": TEMPLATE_INFO['CC_REJECT'],
        "data": {
            "first": {
                "value": "Hi %s, 你的付款凭证被顾问老师驳回，缴费未被确认，请联系顾问询问具体原因，进行修改并重新提交，谢谢！" % (instance.user.name,),
                "color": "#173177"
            },
            "keyword1": {
                "value": instance.user.name,
                "color": "#173177"
            },
            "keyword2": {
                "value": int(instance.pay_fee),
                "color": "#173177"
            },
            "keyword3": {
                "value": str(instance.create_time),
                "color": "#173177"
            },
        }
    }
    return WxInterfaceUtil.send_template_inform(data)


def course_start(confirm_course):
    """
    待上课，推送给学生
    :param confirm_course:课程对象
    :return:
    """
    data = {
        "touser": confirm_course.student.base_openid or confirm_course.student.userinfo_openid,
        "template_id": TEMPLATE_INFO['START_CLASS_REMIND'],
        "data": {
            "first": {
                "value": "%s,你好，你报读的课程即将开课了" % (confirm_course.student.name,),
                "color": "#173177"
            },
            "keyword1": {
                "value": confirm_course.review_course.c_course_name,
                "color": "#173177"
            },
            "keyword2": {
                "value": str(confirm_course.project_course.project.start_date),
                "color": "#173177"
            },
            "remark": {
                "value": "请仔细核对以上信息，并做好上课准备，如有任何疑问，请尽快与你的学服老师取得联系。",
                "color": "#173177"
            }
        }
    }
    return WxInterfaceUtil.send_template_inform(data)


def course_start_cc(confirm_course):
    """
    待上课，推送给cc
    :param confirm_course:课程对象
    :return:
    """
    cc_id = confirm_course.student.cc_id
    cc = UserInfo.objects.filter(id=cc_id).first()
    data = {
        "touser": cc.openid,
        "template_id": TEMPLATE_INFO['START_CLASS_REMIND'],
        "data": {
            "first": {
                "value": "%s你好，学生%s已经添加课程，请留意。" % (cc.username, confirm_course.student.name),
                "color": "#173177"
            },
            "keyword1": {
                "value": confirm_course.review_course.c_course_name,
                "color": "#173177"
            },
            "keyword2": {
                "value": str(confirm_course.project_course.project.start_date),
                "color": "#173177"
            },
            "remark": {
                "value": "",
                "color": "#173177"
            }
        }
    }
    return WxInterfaceUtil.send_template_inform(data)


def send_to_stu(confirm_course):
    """
    成绩单寄出，推送给学生
    :param confirm_course:课程对象
    :return:
    """
    cc_id = confirm_course.student.cc_id
    cc = UserInfo.objects.filter(id=cc_id).first()
    data = {
        "touser": cc.openid,
        "template_id": TEMPLATE_INFO['START_CLASS_REMIND'],
        "data": {
            "first": {
                "value": "%s，你好，你的成绩单正在派送中，请注意查收。" % cc.username,
                "color": "#173177"
            },
            "keyword1": {
                "value": confirm_course.review_course.c_course_name,
                "color": "#173177"
            },
            "keyword2": {
                "value": str(confirm_course.project_course.project.start_date),
                "color": "#173177"
            },
            "remark": {
                "value": "成功上传转学分凭证后，即可参与好评征集活动，最高可获得价值100元的星礼卡哦！",
                "color": "#173177"
            }
        }
    }
    return WxInterfaceUtil.send_template_inform(data)


def review_start_cc(review_course):
    """
    学服添加审课，推送给cc
    :param review_course:审课对象
    :return:
    """
    cc_id = review_course.student.cc_id
    cc = UserInfo.objects.filter(id=cc_id).first()
    data = {
        "touser": cc.openid,
        "template_id": TEMPLATE_INFO['START_CLASS_REMIND'],
        "data": {
            "first": {
                "value": "%s你好，学生%s已经添加审课方案，请留意。" % (cc.username, review_course.student.name),
                "color": "#173177"
            },
            "keyword1": {
                "value": review_course.c_course_name,
                "color": "#173177"
            },
            "keyword2": {
                "value": str(review_course.project.start_date),
                "color": "#173177"
            },
            "remark": {
                "value": "",
                "color": "#173177"
            }
        }
    }
    return WxInterfaceUtil.send_template_inform(data)


def review_start_stu(review_course):
    """
    学服添加审课，推送给学生
    :param review_course:审课对象
    :return:
    """
    data = {
        "touser": review_course.student.base_openid or review_course.student.userinfo_openid,
        "template_id": TEMPLATE_INFO['START_CLASS_REMIND'],
        "data": {
            "first": {
                "value": "%s, 你好，学服老师已为你添加一门审课方案，请尽快上传审课凭据。" % review_course.student.name,
                "color": "#173177"
            },
            "keyword1": {
                "value": review_course.c_course_name,
                "color": "#173177"
            },
            "keyword2": {
                "value": str(review_course.project.start_date),
                "color": "#173177"
            },
            "remark": {
                "value": "审课方案已添加，现参加转发留学群赢礼品活动，即可获得价值50元的星礼卡哦！上传审课凭证后，即可参与留学生朋友圈致富密码活动，音乐平台季度会员免费送！",
                "color": "#173177"
            }
        }
    }
    return WxInterfaceUtil.send_template_inform(data)


def student_submit_order(instance):
    """
    学生提交订单
    :param instance: 订单对象
    :return: 给对应的cc发送微信推送
    """
    user = UserInfo.objects.filter(id=instance.student.cc_id).first()
    order_cart = ProjectOrderRelation.objects.filter(order=instance).values('project__name')
    project_info = ','.join(order.get('project__name') for order in order_cart)
    data = {
        "touser": user.openid,
        "template_id": TEMPLATE_INFO['ORDER_NOTIFICATION'],
        "data": {
            "first": {
                "value": "新订单通知：\n\t%s你好，有学员提交订单，待支付学费，请核对应缴学费金额。" % (user.name,),
                "color": "#173177"
            },
            "keyword1": {
                "value": instance.student.name,
                "color": "#173177"
            },
            "keyword2": {
                "value": "项目: %s" % project_info,
                "color": "#173177"
            },
            "keyword3": {
                "value": str(instance.order_amount),
                "color": "#173177"
            },
            "keyword4": {
                "value": dict(Order.ORDER_STATUS).get(instance.order_status),
                "color": "#173177"
            },
            "keyword5": {
                "value": datetime.datetime.strftime(instance.create_time, "%Y-%m-%d %H:%M:%S"),
                "color": "#173177"
            },
            "remark": {
                "value": "",
                "color": "#173177"
            }
        }
    }
    return WxInterfaceUtil.send_template_inform(data)


def pay_confirm(student):
    """
    财务确认缴费，推送给学服
    :param student: 学生对象
    :return: 微信推送给学服
    """
    user = UserInfo.objects.filter(id=student.server_id).first()
    student_info = "微信号 : %s\n所在学校 : %s\n" % (student.wechat, student.cschool)
    title = "%s你好，一个新数据刚刚分配给你，等待你的学服服务" % user.name
    content = "%s请尽快主动添加学生微信名片为好友，并进入系统后台查看上一流程里市场负责人的相关备注，以便你高效地为学生提供学服服务。" % student_info
    data = {
        "touser": user.openid,
        "template_id": TEMPLATE_INFO['MISSION_INFORM'],
        "data": {
            "first": {
                "value": title,
                "color": "#173177"
            },
            "keyword1": {
                "value": '系统分配',
                "color": "#173177"
            },
            "keyword2": {
                "value": student.channel_name,
                "color": "#173177"
            },
            "keyword3": {
                "value": student.name,
                "color": "#173177"
            },
            "remark": {
                "value": content,
                "color": "#173177"
            }
        }
    }
    return WxInterfaceUtil.send_template_inform(data)


def pay_confirm_cc(order):
    """
    财务确认缴费，推送给cc
    :param order: 订单对象
    :return: 微信推送给学服
    """
    cc = UserInfo.objects.filter(id=order.student.cc_id).first()
    server = UserInfo.objects.filter(id=order.student.server_id).first()
    project_name = ProjectOrderRelation.objects.filter(order=order).values("project__name")
    project_name = ','.join([name['project__name'] for name in project_name])
    data = {
        "touser": cc.openid,
        "template_id": TEMPLATE_INFO['PAY_BILL'],
        "data": {
            "first": {
                "value": "%s你好，订单确认成功，请提醒学员完善档案，系统已指定学服老师%s后续跟进。" % (cc.username,server.username),
                "color": "#173177"
            },
            "keyword1": {
                "value": order.student.name,
                "color": "#173177"
            },
            "keyword2": {
                "value": project_name,
                "color": "#173177"
            },
            "keyword3": {
                "value": int(order.pay_amount),
                "color": "#173177"
            },
            "keyword4": {
                "value": str(order.create_time),
                "color": "#173177"
            },
            "remark": {
                "value": "",
                "color": "#173177"
            }
        }
    }
    return WxInterfaceUtil.send_template_inform(data)


def pay_confirm_stu(order):
    """
    财务确认缴费，推送给学生
    :param order: 订单对象
    :return: 微信推送给学服
    """
    project_name = ProjectOrderRelation.objects.filter(order=order).values("project__name")
    project_name = ','.join([name['project__name'] for name in project_name])
    data = {
        "touser": order.student.base_openid or order.student.userinfo_openid,
        "template_id": TEMPLATE_INFO['PAY_BILL'],
        "data": {
            "first": {
                "value": "%s，你好，订单已确认，请查看。" % order.student.name,
                "color": "#173177"
            },
            "keyword1": {
                "value": order.student.name,
                "color": "#173177"
            },
            "keyword2": {
                "value": project_name,
                "color": "#173177"
            },
            "keyword3": {
                "value": int(order.pay_amount),
                "color": "#173177"
            },
            "keyword4": {
                "value": str(order.create_time),
                "color": "#173177"
            },
            "remark": {
                "value": "",
                "color": "#173177"
            }
        }
    }
    return WxInterfaceUtil.send_template_inform(data)


def financial_rejection(order):
    """
    财务驳回缴费凭据，推送给销售
    :param order: 订单对象
    :return:
    """
    cc = UserInfo.objects.filter(id=order.student.cc_id).first()
    data = {
        "touser": cc.openid,
        "template_id": TEMPLATE_INFO.get('REFUND_REJECT_REMIND'),
        "data": {
            "first": {
                "value": "%s你好，学生%s缴费已被财务驳回，请及时处理。" % (cc.username, order.student.name),
                "color": "#173177"
            },
            "keyword1": {
                "value": "审核失败",
                "color": "#173177"
            },
            "keyword2": {
                "value": order.order_number,
                "color": "#173177"
            },
            "remark": {
                "value": "",
                "color": "#173177"
            }
        }
    }
    return WxInterfaceUtil.send_template_inform(data)


def financial_confirm(order):
    """
    财务确认缴费凭据，推送给销售
    :param order: 订单对象
    :return:
    """
    user_list = UserInfo.objects.filter(id=order.student.cc_id)
    order_cart = ProjectOrderRelation.objects.filter(order=order).values('project__name')
    project_info = ','.join(order.get('project__name') for order in order_cart)
    for user in user_list:
        data = {
            "touser": user.openid,
            "template_id": TEMPLATE_INFO['ORDER_NOTIFICATION'],
            "data": {
                "first": {
                    "value": "新派单通知：\n\t%s你好，订单确认成功，请提醒学员完善档案。系统已指定学服老师%s后续跟进。"
                             % (user.name, order.student.server_name),
                    "color": "#173177"
                },
                "keyword1": {
                    "value": order.student.name,
                    "color": "#173177"
                },
                "keyword2": {
                    "value": "项目: %s" % project_info,
                    "color": "#173177"
                },
                "keyword3": {
                    "value": str(order.order_amount),
                    "color": "#173177"
                },
                "keyword4": {
                    "value": "待完善档案",
                    "color": "#173177"
                },
                "keyword5": {
                    "value": "%s" % str(order.create_time)[:19],
                    "color": "#173177"
                },
                "remark": {
                    "value": "",
                    "color": "#173177"
                }
            }
        }
        return WxInterfaceUtil.send_template_inform(data)


def add_review_course():
    """
    添加审课
    :return: 微信推送给学生
    """
    pass


def upload_review_course_proof():
    """
    学生端上传审课凭证
    :return: 微信推送给cc
    """
    pass


def review_course_status():
    """
    审课状态变化
    :return: 微信推送给学生
    """
    pass


def wait_score_confirm(confirm):
    """
    待成绩确认
    :params: data 课程表对象
    :return: 推送给学生
    """
    data = {
        "touser": confirm.student.base_openid or confirm.student.userinfo_openid,
        "template_id": TEMPLATE_INFO['COURSE_END'],
        "data": {
            "first": {
                "value": "你好，%s，你的成绩已公布：" % confirm.student.name,
                "color": "#173177"
            },
            "keyword1": {
                "value": confirm.review_course.e_course_name,
                "color": "#173177"
            },
            "keyword2": {
                "value": confirm.score,
                "color": "#173177"
            },
            "remark": {
                "value": "请尽快进入学生系统，在24小时内确认所有成绩无误并点击确认成绩，否则系统将自动确认。如有任何疑问，请尽快与你的学服老师取得联系。" ,
                "color": "#173177"
            }
        }
    }
    return WxInterfaceUtil.send_template_inform(data)


def confirm_performance_self(confirm):
    """
    待成绩确认
    :params: data 课程表对象
    :return: 推送给学生
    """
    data = {
        "touser": confirm.student.base_openid or confirm.student.userinfo_openid,
        "template_id": TEMPLATE_INFO['COURSE_END'],
        "data": {
            "first": {
                "value": "你好，%s，你的成绩已公布：" % confirm.student.name,
                "color": "#173177"
            },
            "keyword1": {
                "value": confirm.review_course.e_course_name,
                "color": "#173177"
            },
            "keyword2": {
                "value": confirm.score,
                "color": "#173177"
            },
            "remark": {
                "value": "如有任何疑问，请尽快与你的学服老师取得联系。" ,
                "color": "#173177"
            }
        }
    }
    return WxInterfaceUtil.send_template_inform(data)


def wait_to_transcript():
    """
    带出成绩单
    :return: 推送给学服
    """
    pass


def transcript_to_send():
    """
    待成绩单寄送
    :return: 推送给学服
    """
    pass


def credit_to_transfer(confirm):
    """
    学分转换中
    :return: 推送给学生
    """
    data = {
        "touser": confirm.student.base_openid or confirm.student.userinfo_openid,
        "template_id": TEMPLATE_INFO['CREDIT_CONFIRMATION'],
        "data": {
            "first": {
                "value": "你好，你的成绩单已成功寄出：",
                "color": "#173177"
            },
            "keyword1": {
                "value": confirm.review_course.e_course_name,
                "color": "#173177"
            },
            "keyword2": {
                "value": confirm.student.name,
                "color": "#173177"
            },
            "remark": {
                "value": "请及时进入学生系统查看成绩单的快递单号，并及时跟踪物流情况。成绩单显示签收后，请务必跟进转学分进度。" ,
                "color": "#173177"
            }
        }
    }
    return WxInterfaceUtil.send_template_inform(data)


def transfer_credit_to_confirm(confirm):
    """
    待转学分确认
    :return: 推送给学服
    """
    server = UserInfo.objects.filter(id=confirm.student.server_id).first()
    data = {
        "touser": server.openid,
        "template_id": TEMPLATE_INFO['CREDIT_CONFIRMATION'],
        "data": {
            "first": {
                "value": "你好，%s，你有学生上传了转学分凭证" % server.name,
                "color": "#173177"
            },
            "keyword1": {
                "value": confirm.review_course.e_course_name,
                "color": "#173177"
            },
            "keyword2": {
                "value": confirm.student.name,
                "color": "#173177"
            },
            "remark": {
                "value": "请尽快进入学生系统确认转学分结果。",
                "color": "#173177"
            }
        }
    }
    return WxInterfaceUtil.send_template_inform(data)


def transfer_credit_success(confirm):
    """
    学分转换完成
    :return: 推送给学生
    """
    data = {
        "touser": confirm.student.base_openid or confirm.student.userinfo_openid,
        "template_id": TEMPLATE_INFO['CREDIT_CONFIRMATION'],
        "data": {
            "first": {
                "value": "你好，%s，你的转学分结果已经确认" % confirm.student.name,
                "color": "#173177"
            },
            "keyword1": {
                "value": confirm.review_course.e_course_name,
                "color": "#173177"
            },
            "keyword2": {
                "value": confirm.student.name,
                "color": "#173177"
            },
            "remark": {
                "value": "感谢你一直以来对CS暑校的关注与支持，祝你学业有成，漫漫求学路，我们永相随。",
                "color": "#173177"
            }
        }
    }
    return WxInterfaceUtil.send_template_inform(data)


def add_refund():
    """
    添加退费，添加补缴
    :return: 推送给财务
    """
    pass


def financial_review_refund():
    """
    财务审核退费申请
    :return: 推送给财务
    """
    pass


def refund_success():
    """
    退款成功
    :return: 推送给cc，server ，学生
    """
    pass


def audit_notice(promotion_activity, remark, result):
    """
    学分转换完成
    :return: 推送给学生
    """
    data = {
        "touser": promotion_activity.student.base_openid or promotion_activity.student.userinfo_openid,
        "template_id": TEMPLATE_INFO['AUDIT_NOTICE'],
        "data": {
            "first": {
                "value": "你好，%s，你的活动审核结果已经确认" % promotion_activity.student.name,
                "color": "#173177"
            },
            "keyword1": {
                "value": promotion_activity.get_name_display(),
                "color": "#173177"
            },
            "keyword2": {
                "value": '信息',
                "color": "#173177"
            },
            "keyword3": {
                "value": promotion_activity.student.cschool,
                "color": "#173177"
            },
            "keyword4": {
                "value": promotion_activity.student.name,
                "color": "#173177"
            },
            "keyword5": {
                "value": result,
                "color": "#173177"
            },
            "remark": {
                "value": remark,
                "color": "#173177"
            }
        }
    }
    return WxInterfaceUtil.send_template_inform(data)


TEN_DAYS_SEND = """同学，您好！
您的成绩单已送达学校，转学分流程已开始；
请登录学校系统关注进展，若有疑问可随时联系学服老师。
"""

FIFTEEN_DAYS_SEND = """同学，您好！
你的学分转换已在处理中；
请登录学校系统查看Academic Record，若已成功，请将转学分成功记录上传至CS暑校学生系统；若未成功，请及时联系学服老师处理！

"""